<?php

namespace Extend\UFile;

use Extend\UFile\library\UFile;
use ExtFunc;
use LyApi\Core\Request;
use LyApi\Core\Response;
use LyApi\Core\View;

class Controller
{

    public static function checker($sign = null)
    {
        $res = "";
        $res .= md5("PUB@" . Main::UCLOUD_PUBLIC_KEY);
        $res .= md5("PRI@" . Main::UCLOUD_PRIVATE_KEY);
        $res .= base64_encode(date("Ymd"));
        $res .= md5("UFI@" . $res);

        if ($sign == null) {
            return $res;
        } else {
            return $res == $sign;
        }
    }

    public static function ufile(Request $req, Response $resp)
    {
        $tar = $req->variable[0];

        if ($tar == "getsign") {
            if (ExtFunc::devlogin()) {
                return View::api(Controller::checker());
            }

            return View::api([
                "~" => [
                    "code" => 401,
                    "msg" => "无访问权限"
                ]
            ]);
        }

        if ($tar == "upload") {
            if ($_SERVER['REQUEST_METHOD'] == "POST") {

                if ($req->Post("sign") == "" || !self::checker($req->Post("sign"))) {
                    return View::api([
                        "~" => [
                            "code" => 401,
                            "msg" => "密钥检查错误"
                        ]
                    ]);
                }

                $target = $req->Post("target");
                $bucket = $req->Post("bucket");
                if ($target == "" || $bucket == "") {
                    return View::api([
                        "~" => [
                            "code" => 400,
                            "msg" => "缺少必须参数"
                        ]
                    ]);
                }

                if (isset($_FILES['file']) && $_FILES['file']['error'] == 0) {
                    $file = $_FILES['file'];

                    // "name": "test.zip",
                    // "type": "application/zip",
                    // "tmp_name": "C:\\Windows\\Temp\\phpBDB2.tmp",
                    // "error": 0,
                    // "size": 1069406

                    $name = "UFI@" . md5(uniqid(mt_rand(), true)) . '.tmp';

                    $tmp = ROOT_PATH . '/extend/UFile/cache/' . $name;

                    $mv = move_uploaded_file($file['tmp_name'], $tmp);
                    if (!$mv) {
                        return View::api([
                            "~" => [
                                "code" => 500,
                                "msg" => "暂存文件损坏"
                            ]
                        ]);
                    }

                    list($data, $err) = UFile::putFile($bucket, $target, $tmp);

                    unlink($tmp);

                    if ($err) {
                        return View::api([
                            "~" => [
                                "code" => 500,
                                "msg" => "云端上传失败",
                                "err" => $err
                            ]
                        ]);
                    }

                    return View::api(substr($data['ETag'], 1, strlen($data['ETag']) - 2));
                } else {
                    return View::api([
                        "~" => [
                            "code" => 500,
                            "msg" => "文件上传错误"
                        ]
                    ]);
                }
            } else {
                return View::api([
                    "~" => [
                        "code" => 400,
                        "msg" => "非法的请求方式"
                    ]
                ]);
            }
        } else if ($tar == "delete") {
            if ($req->Post("sign") == "" || !self::checker($req->Post("sign"))) {
                return View::api([
                    "~" => [
                        "code" => 401,
                        "msg" => "密钥检查错误"
                    ]
                ]);
            }


            $target = $req->Post("target");
            $bucket = $req->Post("bucket");
            if ($target == "" || $bucket == "") {
                return View::api([
                    "~" => [
                        "code" => 400,
                        "msg" => "缺少必须参数"
                    ]
                ]);
            }

            list($data, $err) = UFile::deleteFile($bucket, $target);

            if ($err) {
                return View::api([
                    "~" => [
                        "code" => 500,
                        "msg" => "接口返回错误",
                        "err" => $err
                    ]
                ]);
            }

            return $data;
        }
    }
}
